package com.small.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.basis.exception.BusinessException;
import com.small.account.entity.RoleAndPermission;
import com.small.account.mapper.RoleAndPermissionMapper;
import com.small.account.service.IRoleAndPermissionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**

 * 服务实现类

 *
 * @author wangguochao
 * @since 2020-01-06
 */
@Service
public class RoleAndPermissionServiceImpl extends ServiceImpl<RoleAndPermissionMapper, RoleAndPermission> implements IRoleAndPermissionService {

    @Override
    public void roleAddPermission(String roleId, List<String> permissions) {
        if (permissions != null && permissions.size() > 0) {
            QueryWrapper<RoleAndPermission> wrapper = new QueryWrapper<RoleAndPermission>();
            wrapper.eq("role_id", roleId);
            remove(wrapper);
            List<RoleAndPermission> list = new ArrayList<>();
            if (permissions.size()==0){
                throw new BusinessException("没有找到对应权限");
            }
            for (String permission : permissions) {
                RoleAndPermission roleAndPermission = new RoleAndPermission();
                roleAndPermission.setRoleId(roleId);
                roleAndPermission.setPermission(permission);
                list.add(roleAndPermission);
            }
            saveBatch(list);
        }
    }
}
